import Vue from 'vue'

import VeeValidate, { Validator } from 'vee-validate'
import zh from 'vee-validate/dist/locale/zh_CN'

Validator.localize('zh', zh)
Vue.use(VeeValidate)

Validator.extend('required', {
  validate: value => value,
  getMessage: field => {
    return '这是必填的'
  }
})

Validator.extend('iphone', {
  validate: value => {
    return /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/.test(value)
  },
  getMessage: field => {
    return '请输入正确的手机号'
  }
})

Validator.extend('code', {
  validate: value => {
    return /^\d{6}$/.test(value)
  },
  getMessage: field => {
    return '验证码错误'
  }
})

Validator.extend('password', {
  validate: (value, args) => {
    return /^[a-z0-9]{6,16}$/.test(value)
  },
  getMessage: field => {
    return '6-16位数字或字母'
  }
})

Validator.extend('requestPassword', {
  validate: (value, args) => {
    return value === args[0]
  }, // 验证规则
  getMessage: (filed) => '重复密码必须和登录密码一致' // 验证失败的回调
})
